'use strict';
exports.main = async (event, context) => {
  const { username, password } = event;
  
  try {
    const db = uniCloud.database();
    
   
    
    
   
    const checkResult = await db.collection('Accounts')
      .where({ username: username })
      .get();
    
    if (checkResult.data.length > 0) {
      return { success: false, message: '账号已存在' };
    }
    
    
    const encryptedPassword = Buffer.from(password).toString('base64');
    
    const newUser = {
      username: username,
      password: encryptedPassword,
      register_time: Date.now(),
      last_login_time: Date.now()
    };
    
    const addResult = await db.collection('Accounts').add(newUser);
    
    return { 
      success: true, 
      data: { 
        userId: addResult.id,
        username: username
      } 
    };
  } catch (error) {
    console.error('注册失败:', error);
    return { 
      success: false, 
      message: '注册失败: ' + error.message 
    };
  }
};